﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{
    public class Logic
    {
        private const int up = 3, right = 2, left = 1, down = 4;
        private Data data;

        public Logic()
        {
            data = new Data();
        }

        public Rectangle Move(Rectangle rect, int direct, int objspeed)
        {
            //   Console.WriteLine("Before in " + Convert.ToString(rect.Location.X) + "   " + Convert.ToString(rect.Location.X));

            switch (direct)
            {
                case (up): { rect.Location = new Point(rect.Location.X, rect.Location.Y - objspeed); }; break;
                case (down): { rect.Location = new Point(rect.Location.X, rect.Location.Y + objspeed); }; break;
                case (left): { rect.Location = new Point(rect.Location.X - objspeed, rect.Location.Y); }; break;
                case (right): { rect.Location = new Point(rect.Location.X + objspeed, rect.Location.Y); }; break;
            }
            return rect;
            //  Console.WriteLine("After  in" + Convert.ToString(rect.Location.X) + "   " + Convert.ToString(rect.Location.X));

        }

        public bool Collision(Rectangle objmove, Rectangle paintboxform, int direct)
        {


            if (direct == data.up)
            {
                if ((objmove.Location.Y - data.speed) >= 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            if (direct == data.down)
            {
                if ((objmove.Location.Y + objmove.Height + data.speed) <= paintboxform.Size.Height)
                {

                    return true;
                }
                else
                {
                    return false;
                }
            }
            if (direct == data.left)
            {
                if ((objmove.Location.X - data.speed) >= 0)
                {

                    return true;
                }
                else
                {
                    return false;
                }
            }
            if (direct == data.right)
            {
                if ((objmove.Location.X + objmove.Width + data.speed) <= paintboxform.Size.Width)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
        }

        public bool CheckFreePosition(int x, int y, List<Brick> listbrick, List<Tank> enemys, List<Water> listwater)
        {
            int k = 0;
            for (int i = 0; i < listbrick.Count; i++)
            {
                if ((listbrick[i].Rect.Location.X == x) && (listbrick[i].Rect.Location.Y == y))
                {
                    k++;
                }
                if ((listbrick[i].Rect.Location.X == (x + 10)) && (listbrick[i].Rect.Location.Y == y))
                {
                    k++;
                }
                if ((listbrick[i].Rect.Location.X == (x + 20)) && (listbrick[i].Rect.Location.Y == y))
                {
                    k++;
                }
                if ((listbrick[i].Rect.Location.X == (x)) && (listbrick[i].Rect.Location.Y == (y + 10)))
                {
                    k++;
                }
                if ((listbrick[i].Rect.Location.X == (x + 10)) && (listbrick[i].Rect.Location.Y == (y + 10)))
                {
                    k++;
                }
                if ((listbrick[i].Rect.Location.X == (x + 20)) && (listbrick[i].Rect.Location.Y == (y + 10)))
                {
                    k++;
                }
                if ((listbrick[i].Rect.Location.X == (x)) && (listbrick[i].Rect.Location.Y == (y + 20)))
                {
                    k++;
                }
                if ((listbrick[i].Rect.Location.X == (x + 10)) && (listbrick[i].Rect.Location.Y == (y + 20)))
                {
                    k++;
                }
                if ((listbrick[i].Rect.Location.X == (x + 20)) && (listbrick[i].Rect.Location.Y == (y + 20)))
                {
                    k++;
                }

            }

            for (int i = 0; i < listwater.Count; i++)
            {
                if ((listwater[i].Rect.Location.X == x) && (listwater[i].Rect.Location.Y == y))
                {
                    k++;
                }
                if ((listwater[i].Rect.Location.X == (x + 10)) && (listwater[i].Rect.Location.Y == y))
                {
                    k++;
                }
                if ((listwater[i].Rect.Location.X == (x + 20)) && (listwater[i].Rect.Location.Y == y))
                {
                    k++;
                }
                if ((listwater[i].Rect.Location.X == (x)) && (listwater[i].Rect.Location.Y == (y + 10)))
                {
                    k++;
                }
                if ((listwater[i].Rect.Location.X == (x + 10)) && (listwater[i].Rect.Location.Y == (y + 10)))
                {
                    k++;
                }
                if ((listwater[i].Rect.Location.X == (x + 20)) && (listwater[i].Rect.Location.Y == (y + 10)))
                {
                    k++;
                }
                if ((listwater[i].Rect.Location.X == (x)) && (listwater[i].Rect.Location.Y == (y + 20)))
                {
                    k++;
                }
                if ((listwater[i].Rect.Location.X == (x + 10)) && (listwater[i].Rect.Location.Y == (y + 20)))
                {
                    k++;
                }
                if ((listwater[i].Rect.Location.X == (x + 20)) && (listwater[i].Rect.Location.Y == (y + 20)))
                {
                    k++;
                }

            }

            for (int j = 0; j < enemys.Count; j++)
            {
                if (((x >= (enemys[j].tank.Location.X))
             && ((x) < (enemys[j].tank.Location.X + enemys[j].tank.Width))
                && ((y) >= (enemys[j].tank.Location.Y))
                     && ((y) < (enemys[j].tank.Location.Y + enemys[j].tank.Height)))

       || (((x + data.tank_width) > (enemys[j].tank.Location.X))
             && ((x + data.tank_width) <= (enemys[j].tank.Location.X + enemys[j].tank.Width))
                && ((y) > (enemys[j].tank.Location.Y))
                     && ((y) < (enemys[j].tank.Location.Y + enemys[j].tank.Height)))

                     || (((x + data.tank_width) > (enemys[j].tank.Location.X))
                            && ((x + data.tank_width) <= (enemys[j].tank.Location.X + enemys[j].tank.Width))
                                 && ((y + data.tank_height) > (enemys[j].tank.Location.Y))
                                       && ((y + data.tank_height) <= (enemys[j].tank.Location.Y + enemys[j].tank.Height)))

                                       || (((x) >= (enemys[j].tank.Location.X))
                            && ((x) < (enemys[j].tank.Location.X + enemys[j].tank.Width))
                                 && ((y + data.tank_height) > (enemys[j].tank.Location.Y))
                                       && ((y + data.tank_height) <= (enemys[j].tank.Location.Y + enemys[j].tank.Height))))
                {
                    k++;
                }
                #region temp
                //if ((enemys[i].tank.Location.X == x) && (enemys[i].tank.Location.Y == y))
                //{
                //    k++;
                //}
                //if ((enemys[i].tank.Location.X == (x + 10)) && (listbrick[i].Rect.Location.Y == (y)))
                //{
                //    k++;
                //}
                //if ((enemys[i].tank.Location.X == (x + 20)) && (listbrick[i].Rect.Location.Y == (y)))
                //{
                //    k++;
                //}
                //if ((enemys[i].tank.Location.X == (x)) && (listbrick[i].Rect.Location.Y == (y + 10)))
                //{
                //    k++;
                //} if ((enemys[i].tank.Location.X == (x + 10)) && (listbrick[i].Rect.Location.Y == (y + 10)))
                //{
                //    k++;
                //} if ((enemys[i].tank.Location.X == (x + 20)) && (listbrick[i].Rect.Location.Y == (y + 10)))
                //{
                //    k++;
                //} if ((enemys[i].tank.Location.X == (x)) && (listbrick[i].Rect.Location.Y == (y + 20)))
                //{
                //    k++;
                //} if ((enemys[i].tank.Location.X == (x + 10)) && (listbrick[i].Rect.Location.Y == (y + 20)))
                //{
                //    k++;
                //} if ((enemys[i].tank.Location.X == (x + 20)) && (listbrick[i].Rect.Location.Y == (y + 20)))
                //{
                //    k++;
                //}
                #endregion temp
            }

            if (k == 0)
            {
                //MessageBox.Show("true");
                return false;
            }
            else
            {
                return true;
            }
        }

        public int CheckEnemy(Rectangle rocket, int InOrOut, int iam, int type, List<Tank> enemys)
        {
            for (int j = 0; j < enemys.Count; j++)
            {
                // if (j != iam)
                //  if (enemys[j].MyTank != 1)
                {

                    if (InOrOut == 0)
                    {
                        if (type == 1)
                        {
                            if (((rocket.Location.X) >= enemys[j].tank.Location.X)
                                && ((rocket.Location.X) <= (enemys[j].tank.Location.X + enemys[j].tank.Width))
                                    && ((rocket.Location.Y) >= enemys[j].tank.Location.Y)
                                        && ((rocket.Location.Y) <= enemys[j].tank.Location.Y + enemys[j].tank.Height))
                            {
                                return j;
                            }
                        }
                        else
                        {
                            if (enemys[0].MyTank == 1)
                            {
                                if (((rocket.Location.X) >= enemys[0].tank.Location.X)
                                    && ((rocket.Location.X) <= (enemys[0].tank.Location.X + enemys[0].tank.Width))
                                        && ((rocket.Location.Y) >= enemys[0].tank.Location.Y)
                                            && ((rocket.Location.Y) <= enemys[0].tank.Location.Y + enemys[0].tank.Height))
                                {
                                    return enemys.IndexOf(enemys[0]);
                                }
                            }
                        }
                    }

                    if ((InOrOut > 0))
                    {
                        if (iam != j)
                        {
                            if (((rocket.Location.X >= (enemys[j].tank.Location.X))
                                      && ((rocket.Location.X) < (enemys[j].tank.Location.X + enemys[j].tank.Width))
                                         && ((rocket.Location.Y) >= (enemys[j].tank.Location.Y))
                                              && ((rocket.Location.Y) < (enemys[j].tank.Location.Y + enemys[j].tank.Height)))

                                || (((rocket.Location.X + rocket.Width) > (enemys[j].tank.Location.X))
                                      && ((rocket.Location.X + rocket.Width) <= (enemys[j].tank.Location.X + enemys[j].tank.Width))
                                         && ((rocket.Location.Y) > (enemys[j].tank.Location.Y))
                                              && ((rocket.Location.Y) < (enemys[j].tank.Location.Y + enemys[j].tank.Height)))

                                              || (((rocket.Location.X + rocket.Width) > (enemys[j].tank.Location.X))
                                                     && ((rocket.Location.X + rocket.Width) <= (enemys[j].tank.Location.X + enemys[j].tank.Width))
                                                          && ((rocket.Location.Y + rocket.Height) > (enemys[j].tank.Location.Y))
                                                                && ((rocket.Location.Y + rocket.Height) <= (enemys[j].tank.Location.Y + enemys[j].tank.Height)))

                                                                || (((rocket.Location.X) >= (enemys[j].tank.Location.X))
                                                     && ((rocket.Location.X) < (enemys[j].tank.Location.X + enemys[j].tank.Width))
                                                          && ((rocket.Location.Y + rocket.Height) > (enemys[j].tank.Location.Y))
                                                                && ((rocket.Location.Y + rocket.Height) <= (enemys[j].tank.Location.Y + enemys[j].tank.Height))))
                            {
                                return j;
                            }
                        }


                    }
                }
            }
            return (-1);
        }


        public int CheckBrick(Rectangle rocket, int InOrOut, int dy, List<Brick> listbrick)
        {
            for (int k = 0; k < listbrick.Count; k++)
            {

                if (InOrOut == 0)
                {

                    if (((rocket.Location.X >= (listbrick[k].Rect.Location.X))
                           && (rocket.Location.X <= (listbrick[k].Rect.Location.X + listbrick[k].Rect.Width))
                              && (rocket.Location.Y >= (listbrick[k].Rect.Location.Y))
                                 && (rocket.Location.Y <= (listbrick[k].Rect.Location.Y + listbrick[k].Rect.Height))))
                    {
                        return k;
                    }
                }

                if (InOrOut > 0)
                {
                    if (((listbrick[k].Rect.Location.X) >= rocket.Location.X)
                              && ((listbrick[k].Rect.Location.X + listbrick[k].Rect.Width) <= (rocket.Location.X + rocket.Width))
                                 && ((listbrick[k].Rect.Location.Y) >= (rocket.Location.Y))
                                      && ((listbrick[k].Rect.Location.Y + listbrick[k].Rect.Height) <= (rocket.Location.Y + rocket.Height)))
                    {
                        return k;
                    }

                }

            }

            return (-1);
        }

        public int CheckWater(Rectangle rocket, int InOrOut, int dy, List<Water> listbrick)
        {
            for (int k = 0; k < listbrick.Count; k++)
            {

                if (InOrOut == 0)
                {

                    if (((rocket.Location.X >= (listbrick[k].Rect.Location.X))
                           && (rocket.Location.X <= (listbrick[k].Rect.Location.X + listbrick[k].Rect.Width))
                              && (rocket.Location.Y >= (listbrick[k].Rect.Location.Y))
                                 && (rocket.Location.Y <= (listbrick[k].Rect.Location.Y + listbrick[k].Rect.Height))))
                    {
                        return k;
                    }
                }

                if (InOrOut > 0)
                {
                    if (((listbrick[k].Rect.Location.X) >= rocket.Location.X)
                              && ((listbrick[k].Rect.Location.X + listbrick[k].Rect.Width) <= (rocket.Location.X + rocket.Width))
                                 && ((listbrick[k].Rect.Location.Y) >= (rocket.Location.Y))
                                      && ((listbrick[k].Rect.Location.Y + listbrick[k].Rect.Height) <= (rocket.Location.Y + rocket.Height)))
                    {
                        return k;
                    }

                }

            }

            return (-1);
        }

        public int CheckBonus(Rectangle bonus, List<Bonus> listbonus)
        {
            for (int j = 0; j < listbonus.Count; j++)
            {
                if (((bonus.Location.X >= (listbonus[j].RBonus.X))
                                             && ((bonus.Location.X) < (listbonus[j].RBonus.Location.X + listbonus[j].RBonus.Width))
                                                && ((bonus.Location.Y) >= (listbonus[j].RBonus.Location.Y))
                                                     && ((bonus.Location.Y) < (listbonus[j].RBonus.Location.Y + listbonus[j].RBonus.Height)))

                                       || (((bonus.Location.X + bonus.Width) > (listbonus[j].RBonus.Location.X))
                                             && ((bonus.Location.X + bonus.Width) <= (listbonus[j].RBonus.Location.X + listbonus[j].RBonus.Width))
                                                && ((bonus.Location.Y) > (listbonus[j].RBonus.Location.Y))
                                                     && ((bonus.Location.Y) < (listbonus[j].RBonus.Location.Y + listbonus[j].RBonus.Height)))

                                                     || (((bonus.Location.X + bonus.Width) > (listbonus[j].RBonus.Location.X))
                                                            && ((bonus.Location.X + bonus.Width) <= (listbonus[j].RBonus.Location.X + listbonus[j].RBonus.Width))
                                                                 && ((bonus.Location.Y + bonus.Height) > (listbonus[j].RBonus.Location.Y))
                                                                       && ((bonus.Location.Y + bonus.Height) <= (listbonus[j].RBonus.Location.Y + listbonus[j].RBonus.Height)))

                                                                       || (((bonus.Location.X) >= (listbonus[j].RBonus.Location.X))
                                                            && ((bonus.Location.X) < (listbonus[j].RBonus.Location.X + listbonus[j].RBonus.Width))
                                                                 && ((bonus.Location.Y + bonus.Height) > (listbonus[j].RBonus.Location.Y))
                                                                       && ((bonus.Location.Y + bonus.Height) <= (listbonus[j].RBonus.Location.Y + listbonus[j].RBonus.Height))))
                {
                    return j;
                }
            }

            return (-1);

        }
    }
}